home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / debugger / bdm-linu.0 / bdm-linu / bdm-linux / bdmcpu32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-27  |  8.5 KB  |  383 lines

  1. /* bdmcpu32.c - routines to talk to CPU32 target system
  2.  * Copyright (C) 1992 by Scott Howard, all rights reserved
  3.  * Permission is hereby granted to freely copy and use this code or derivations thereof
  4.  * as long as no charge is made to anyone for its use
  5.  */
  6.  
  7. #include    <stdio.h>
  8. #include    <stdarg.h>
  9. #include    "bdmcalls.h"
  10. #include    "bdm.h"
  11. #include    "trgtstat.h"
  12. #include    "regs-32.h"
  13. #include    "bdmerror.h"
  14.  
  15. #define         BDM_RDREG       0x2180
  16. #define         BDM_WRREG       0x2080
  17. #define         BDM_RSREG       0x2580
  18. #define         BDM_WSREG       0x2480
  19. #define         BDM_READ        0x1900
  20. #define         BDM_WRITE       0x1800
  21. #define         BDM_DUMP        0x1D00
  22. #define         BDM_FILL        0x1C00
  23. #define         BDM_GO          0x0C00
  24. #define         BDM_CALL        0x0800
  25. #define         BDM_RST         0x0400
  26. #define         BDM_NOP         0x0000
  27.  
  28. #define         BDM_BYTESIZE    0x00
  29. #define         BDM_WORDSIZE    0x40
  30. #define         BDM_LONGSIZE    0x80
  31. #define         BDM_RDBIT       0x100
  32.  
  33. #define         BDM_RPC         0x0
  34. #define         BDM_PCC         0x1
  35. #define         BDM_SR          0xb
  36. #define         BDM_USP         0xc
  37. #define         BDM_SSP         0xd
  38. #define         BDM_SFC         0xe
  39. #define         BDM_DFC         0xf
  40. #define         BDM_ATEMP       0x8
  41. #define         BDM_FAR         0x9
  42. #define         BDM_VBR         0xa
  43.  
  44. #define         BDM_NOTREADY    0x10000
  45. #define         BDM_BERR        0x10001
  46. #define         BDM_ILLEGAL     0x1FFFF
  47. #define         BDM_CMDCMPLTE   0x0FFFF
  48.  
  49. static void bdm_clrerror (void);
  50. static BYTE fc_set = 0, fc = 5;
  51. static LONG old_sfc, old_dfc;
  52. static char last_rw;
  53. static LONG last_addr;
  54. unsigned go_cmd = BDM_GO, CommandBitCount = 17;
  55. static Initted = 0;
  56. char RegsValid;
  57.  
  58. int set_fc (void)
  59. {
  60.     if (fc_set) return 0;
  61.     old_sfc = (int) GetReg (REG_SFC);
  62.     old_dfc = (int) GetReg (REG_DFC);
  63.     PutReg (REG_SFC, (LONG) fc);
  64.     PutReg (REG_DFC, (LONG) fc);
  65.     return fc_set = 1;
  66. }
  67.  
  68. int restore_fc (void)
  69. {
  70.     if (!fc_set) return 0;
  71.     PutReg (REG_SFC, (LONG) old_sfc);
  72.     PutReg (REG_DFC, (LONG) old_dfc);
  73.     fc_set = 0;
  74.     return 1;
  75. }
  76.  
  77. /* ValidPorts returns unsigned integer where each bit position
  78.  * indicates an installed LPT port (Bit 0 = LPT1, bit 1 = LPT2, etc)
  79.  * we will need eventually to look in /proc/ioports.  for now, return 1
  80.  */
  81.  
  82. unsigned ValidPorts (void)
  83. {
  84.     return 1;
  85. }
  86.  
  87. void SetFC (unsigned NewFC)
  88. {
  89.     if (NewFC == UserData || NewFC == UserCode
  90.     || NewFC == SupervisorData || NewFC == SupervisorCode)
  91.         fc = NewFC;
  92. }
  93.  
  94. /* DeInit must be called before program quits or shells to DOS
  95.  * un-do any bad things which have been done to talk to target
  96.  */
  97.  
  98. void DeInit (void)
  99. {
  100.     if (Initted) bdm_deinit ();
  101.     Initted = 0;
  102. }
  103.  
  104. /* Init initializes parallel port to talk to target */
  105.  
  106. int Init (unsigned port, unsigned baud)
  107. {
  108.     DeInit ();
  109.     if (!(ValidPorts () & (1 << (port - 1)))) return -1;
  110.  
  111.     bdm_init (0x378, baud);
  112.     GetStatus ();
  113.     Initted = 1;
  114.     return 0;
  115. }
  116.  
  117. /* bdm_error handles call to DriverError if error detected by bdm routines */
  118.  
  119. void bdm_error (int type)
  120. {
  121.     if ((type > BDM_FAULT_BERR) || (type < 0)) type = 0;
  122.     if (type == BDM_FAULT_BERR) restore_fc ();
  123.     DriverError (type, last_rw, last_addr);
  124. }
  125. static void bdm_clrerror (void)
  126. {
  127.     int count;
  128.     long response;
  129.  
  130.     bdm_clk (BDM_NOP, CommandBitCount);
  131.     bdm_clk (BDM_NOP, CommandBitCount);
  132.     bdm_clk (BDM_NOP, CommandBitCount);
  133.     bdm_clk (BDM_NOP, CommandBitCount);
  134.     while (bdm_clk (0, 1)) ;
  135.     while (!bdm_clk (0, 1)) ;
  136.     bdm_clk (0, 15);
  137. }
  138.  
  139. static LONG bdm_read (LONG addr, int pcount, WORD cmd, ...)
  140. {
  141.     _G_va_list arg;
  142.     int err,count;
  143.     LONG response,result;
  144.  
  145.     last_addr = addr;
  146.     last_rw = 1;
  147.     result = 0L;
  148.         if (!Initted) bdm_error (BDM_FAULT_PORT);
  149.     for (err = 3; err; err--)
  150.     {
  151.         count = pcount;
  152.         va_start (arg,cmd);
  153.         response = bdm_clk (cmd, CommandBitCount);
  154.         while (count--)
  155.             if ((response = bdm_clk (va_arg(arg,WORD), CommandBitCount)) > BDM_NOTREADY)
  156.             {
  157.                 if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  158.                 continue;
  159.             }
  160.         for (count = (cmd & BDM_LONGSIZE) ? 2 : 1; count; count--)
  161.         {
  162.             while ((response = bdm_clk (BDM_NOP, CommandBitCount)) == BDM_NOTREADY) ;
  163.             if (response < BDM_NOTREADY)
  164.             {
  165.                 result <<= 16;
  166.                 result |= response;
  167.             }
  168.             else
  169.             {
  170.                 if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  171.                 break;
  172.             }
  173.         }
  174.         if (response > BDM_NOTREADY)
  175.         {
  176.             if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  177.             bdm_clrerror ();
  178.             continue;
  179.         }
  180.         va_end (arg);
  181.         break;
  182.     }
  183.         if (!err) bdm_error (BDM_FAULT_UNKNOWN);
  184.     response = bdm_clk (BDM_NOP, CommandBitCount);
  185.     if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  186.     return result;
  187. }
  188.  
  189. static void bdm_write (LONG addr, int pcount, WORD cmd, ...)
  190. {
  191.     va_list arg;
  192.     int err,count;
  193.     LONG response,result;
  194.  
  195.     last_addr = addr;
  196.     last_rw = 0;
  197.     if (!Initted) bdm_error (BDM_FAULT_PORT);
  198.     for (err = 3; err; err--)
  199.     {
  200.         count = pcount;
  201.         va_start (arg,cmd);
  202.         response = bdm_clk (cmd, CommandBitCount);
  203.         while (count--)
  204.             if ((response = bdm_clk (va_arg(arg,WORD), CommandBitCount)) > BDM_NOTREADY)
  205.             {
  206.                 if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  207.                 bdm_clrerror ();
  208.                 continue;
  209.             }
  210.         while ((response = bdm_clk (BDM_NOP, CommandBitCount)) == BDM_NOTREADY) ;
  211.         if (response == BDM_CMDCMPLTE) break;
  212.         else if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  213.  
  214.     }
  215.     va_end (arg);
  216.     if (!err) bdm_error (BDM_FAULT_UNKNOWN);
  217.     response = bdm_clk (BDM_NOP, CommandBitCount);
  218.     if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  219. }
  220.  
  221. void RunChip (LONG where)
  222. {
  223.     StopChip ();
  224.     if (where)
  225.         bdm_write (where, 2, BDM_WSREG + BDM_RPC, (WORD) (where >> 16), (WORD) where);
  226.     bdm_clk (BDM_GO, CommandBitCount);
  227. }
  228.  
  229. LONG GetByte (LONG x)
  230. {
  231.     int frozen,save,c;
  232.     LONG result;
  233.  
  234.     frozen = StopChip ();
  235.     save = set_fc ();
  236.     result = bdm_read (x, 2, BDM_READ, (WORD) (x >> 16), (WORD) x);
  237.     if (save) restore_fc ();
  238.     if (frozen) RunChip (0L);
  239.     return result;
  240. }
  241.  
  242. void PutByte (LONG x, BYTE y)
  243. {
  244.     int frozen,save;
  245.  
  246.     frozen = StopChip ();
  247.     save = set_fc ();
  248.     bdm_write (x, 3, BDM_WRITE, (WORD) (x >> 16), (WORD) x, (WORD) y);
  249.     if (save) restore_fc ();
  250.     if (frozen) RunChip (0L);
  251. }
  252.  
  253. void FillByte (LONG x, BYTE y)
  254. {
  255.     int frozen,save;
  256.  
  257.     frozen = StopChip ();
  258.     save = set_fc ();
  259.     bdm_write (x, 1, BDM_FILL, (WORD) y);
  260.     if (save) restore_fc ();
  261.     if (frozen) RunChip (0L);
  262. }
  263.  
  264. LONG DumpByte (LONG x)
  265. {
  266.     return GetByte (x);
  267. }
  268.  
  269. LONG GetWord (LONG x)
  270. {
  271.     int frozen,save;
  272.     LONG result;
  273.  
  274.     frozen = StopChip ();
  275.     save = set_fc ();
  276.     result = bdm_read (x, 2, BDM_READ + BDM_WORDSIZE, (WORD) (x >> 16), (WORD) x);
  277.     if (save) restore_fc ();
  278.     if (frozen) RunChip (0L);
  279.     return result;
  280. }
  281.  
  282. void PutWord (LONG x, WORD y)
  283. {
  284.     int frozen,save;
  285.  
  286.     frozen = StopChip ();
  287.     save = set_fc ();
  288.     bdm_write (x, 3, BDM_WRITE + BDM_WORDSIZE, (WORD) (x >> 16), (WORD) x, y);
  289.     if (save) restore_fc ();
  290.     if (frozen) RunChip (0L);
  291. }
  292.  
  293. void FillWord (LONG x, WORD y)
  294. {
  295.     int frozen,save;
  296.  
  297.     frozen = StopChip ();
  298.     save = set_fc ();
  299.     bdm_write (x, 1, BDM_FILL + BDM_WORDSIZE, y);
  300.     if (save) restore_fc ();
  301.     if (frozen) RunChip (0L);
  302. }
  303.  
  304. LONG DumpWord (LONG x)
  305. {
  306.     return GetWord (x);
  307. }
  308.  
  309. LONG GetLong (LONG x)
  310. {
  311.     int frozen,save;
  312.     LONG result;
  313.  
  314.     frozen = StopChip ();
  315.     save = set_fc ();
  316.     result = bdm_read (x, 2, BDM_READ + BDM_LONGSIZE, (WORD) (x >> 16), (WORD) x);
  317.     if (save) restore_fc ();
  318.     if (frozen) RunChip (0L);
  319.     return result;
  320. }
  321.  
  322. void PutLong (LONG x, LONG y)
  323. {
  324.     int frozen,save;
  325.  
  326.     frozen = StopChip ();
  327.     save = set_fc ();
  328.     bdm_write (x, 4, BDM_WRITE + BDM_LONGSIZE, (WORD) (x >> 16), (WORD) x, (WORD) (y >> 16), (WORD) y);
  329.     if (save) restore_fc ();
  330.     if (frozen) RunChip (0L);
  331. }
  332.  
  333. void FillLong (LONG x, LONG y)
  334. {
  335.     int frozen,save;
  336.  
  337.     frozen = StopChip ();
  338.     save = set_fc ();
  339.     bdm_write (x, 2, BDM_FILL + BDM_LONGSIZE, (WORD) (y >> 16), (WORD) y);
  340.     if (save) restore_fc ();
  341.     if (frozen) RunChip (0L);
  342. }
  343.  
  344. LONG DumpLong (LONG x)
  345. {
  346.     return GetLong (x);
  347. }
  348.  
  349. static WORD RegCodes [] = {
  350. BDM_WSREG + BDM_RPC, BDM_WSREG + BDM_USP,
  351. BDM_WSREG + BDM_SSP, BDM_WSREG + BDM_VBR,
  352. BDM_WSREG + BDM_SR, BDM_WSREG + BDM_SFC,
  353. BDM_WSREG + BDM_DFC, BDM_WSREG + BDM_ATEMP,
  354.  
  355. BDM_WRREG + 0, BDM_WRREG + 1, BDM_WRREG + 2, BDM_WRREG + 3,
  356. BDM_WRREG + 4, BDM_WRREG + 5, BDM_WRREG + 6, BDM_WRREG + 7,
  357.  
  358. BDM_WRREG + 8, BDM_WRREG + 9, BDM_WRREG + 10, BDM_WRREG + 11,
  359. BDM_WRREG + 12, BDM_WRREG + 13, BDM_WRREG + 14, BDM_WRREG + 15
  360. };
  361.  
  362. LONG GetReg (unsigned which)
  363. {
  364.     int frozen;
  365.     LONG result;
  366.  
  367.     frozen = StopChip ();
  368.     result = bdm_read (0L,0,BDM_RDBIT + RegCodes [which]);
  369.     if (frozen) RunChip (0L);
  370.     return result;
  371. }
  372.  
  373. void PutReg (unsigned which, LONG data)
  374. {
  375.     int frozen;
  376.  
  377.     frozen = StopChip ();
  378.     bdm_write (0L, 2,RegCodes [which], (WORD) (data >> 16), (WORD) data);
  379.     if (frozen) RunChip (0L);
  380. }
  381.  
  382. /* end of bdmcpu32.c */
  383.